import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

public class Main {
    public static void main(String[] args) throws InterruptedException, IOException {
        // test01();
        // test02();
        // 这个方法会报错, 因为点击的是百度中新闻超链接, 没有放在 form 标签中
        // test03();
        // test04();
        // test05();
        // 显示等待, 等待的是一定的条件(程序员自己设定的)
        // 而隐式是等待页面所有的元素
        // test06();
        // test07();
        // test08();
        // test09();
        // page01();
        // page02();
        // page03();
        // 针对 alert 操作
        // page04();
        // page05();
        // test10();
        // test11();
        // 截图操作
        test12();
    }

    private static void test12() throws InterruptedException, IOException {
        ChromeOptions options = new ChromeOptions();
        // 允许所有的请求
        options.addArguments("--remote-allow-origins=*");
        ChromeDriver driver = new ChromeDriver(options);
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        driver.get("https://www.baidu.com/");
        driver.findElement(By.cssSelector("#kw")).sendKeys("软件测试");
        driver.findElement(By.cssSelector("#su")).click();
        Thread.sleep(2000);

        File file = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(file, new File("F:\\Code\\exercises\\Test\\t_20231023\\src\\main\\picture\\软件测试截图.png"));
    }

    private static void test11() throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        // 允许所有的请求
        options.addArguments("--remote-allow-origins=*");
        ChromeDriver driver = new ChromeDriver(options);
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        driver.get("https://www.baidu.com/");
        driver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();

        // 通过 getWindowHandles 是获取所有的窗口句柄
        // getWindowHandle 是获取 get 打开的窗口句柄
        System.out.println(driver.getWindowHandle());
        Set<String> handles = driver.getWindowHandles();
        String targetHandle = "";

        for (String handle : handles) {
            targetHandle = handle;
        }
        driver.switchTo().window(targetHandle);
        Thread.sleep(3000);

        driver.findElement(By.cssSelector("#ww")).sendKeys("新闻联播");
        driver.findElement(By.cssSelector("#s_btn_wr")).click();
    }

    private static void test10() throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        // 允许所有的请求
        options.addArguments("--remote-allow-origins=*");
        ChromeDriver driver = new ChromeDriver(options);
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        driver.get("https://www.baidu.com/");
        driver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
        Thread.sleep(3000);
        // quit 是关闭了整个浏览器, 会清空缓存
        // driver.quit();
        // close 只是关闭了当前的页面(关闭 get 方法打开的页面), 不会清空缓存
        driver.close();
    }

    private static void page05() {
        ChromeOptions options = new ChromeOptions();
        // 允许所有的请求
        options.addArguments("--remote-allow-origins=*");
        ChromeDriver driver = new ChromeDriver(options);
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        driver.get("F:\\Code\\exercises\\Test\\t_20231023\\src\\main\\page\\test05.html");

        driver.findElement(By.cssSelector("input")).sendKeys("C:\\Users\\AliceMana\\Pictures\\Saved Pictures\\WnjwPpgV_400x400.jpg");
    }

    private static void page04() throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        // 允许所有的请求
        options.addArguments("--remote-allow-origins=*");
        ChromeDriver driver = new ChromeDriver(options);
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        driver.get("F:\\Code\\exercises\\Test\\t_20231023\\src\\main\\page\\test04.html");

        // 点击弹窗
        driver.findElement(By.cssSelector("body > button")).click();
        Thread.sleep(2000);

        // alert 弹窗取消
        driver.switchTo().alert().dismiss();
        Thread.sleep(2000);

        // 点击按钮
        driver.findElement(By.cssSelector("body > button")).click();
        Thread.sleep(2000);

        // 输入 test
        driver.switchTo().alert().sendKeys("test");
        Thread.sleep(2000);

        // alert 弹窗确认
        driver.switchTo().alert().accept();
    }

    private static void page03() {
        ChromeOptions options = new ChromeOptions();
        // 允许所有的请求
        options.addArguments("--remote-allow-origins=*");
        ChromeDriver driver = new ChromeDriver(options);
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        driver.get("F:\\Code\\exercises\\Test\\t_20231023\\src\\main\\page\\test03.html");

        WebElement element = driver.findElement(By.cssSelector("#ShippingMethod"));
        Select select = new Select(element);
        // 通过下标来选择
        // select.selectByIndex(3);
        // 通过 val 来选择
        select.selectByValue("12.51");
    }

    private static void page02() {
        ChromeOptions options = new ChromeOptions();
        // 允许所有的请求
        options.addArguments("--remote-allow-origins=*");
        ChromeDriver driver = new ChromeDriver(options);
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        driver.get("F:\\Code\\exercises\\Test\\t_20231023\\src\\main\\page\\test02.html");
        driver.switchTo().frame("f1");
        driver.findElement(By.cssSelector("body > div > div > a")).click();
    }

    private static void page01() {
        ChromeOptions options = new ChromeOptions();
        // 允许所有的请求
        options.addArguments("--remote-allow-origins=*");
        ChromeDriver driver = new ChromeDriver(options);
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        driver.get("F:\\Code\\exercises\\Test\\t_20231023\\src\\main\\page\\test01.html");
        List<WebElement> elements = driver.findElements(By.cssSelector("input"));
        for (WebElement element : elements) {
            // 如果每个元素的 type 值等于 checkbox 则进行点击
            if (element.getAttribute("type").equals("checkbox")) {
                element.click();
            }
            // 否则什么都不干
        }
    }

    private static void test09() throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        // 允许所有的请求
        options.addArguments("--remote-allow-origins=*");
        ChromeDriver driver = new ChromeDriver(options);
        // 打开百度首页
        driver.get("https://www.baidu.com");

        // 输入 520
        driver.findElement(By.cssSelector("#kw")).sendKeys("520");
        driver.findElement(By.cssSelector("#su")).click();
        Thread.sleep(2000);

        // 找到图片按钮
        WebElement element = driver.findElement(By.cssSelector("#s_tab > div > a.s-tab-item.s-tab-item_1CwH-.s-tab-pic_p4Uej.s-tab-pic"));

        // 右击
        Actions actions = new Actions(driver);
        Thread.sleep(2000);
        // moveToElement: 移动到此元素的位置
        // contextClick: 右击
        // perform: 执行(必要)
        actions.moveToElement(element).contextClick().perform();

    }

    private static void test08() throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        // 允许所有的请求
        options.addArguments("--remote-allow-origins=*");
        ChromeDriver driver = new ChromeDriver(options);
        // 打开百度首页
        driver.get("https://www.baidu.com");

        // 输入 520
        driver.findElement(By.cssSelector("#kw")).sendKeys("520");
        Thread.sleep(2000);

        // ctrl + A
        driver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "A");
        Thread.sleep(2000);

        // ctrl + X
        driver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "X");
        Thread.sleep(2000);

        // ctrl + V
        driver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "V");
        Thread.sleep(2000);
    }


    private static void test07() throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        // 允许所有的请求
        options.addArguments("--remote-allow-origins=*");
        ChromeDriver driver = new ChromeDriver(options);
        // 打开百度首页
        driver.get("https://www.baidu.com");
        // 搜索
        driver.findElement(By.cssSelector("#kw")).sendKeys("520");
        driver.findElement(By.cssSelector("#su")).click();
        Thread.sleep(1000);
        // 浏览器后退
        driver.navigate().back();
        Thread.sleep(1000);
        // 刷新
        driver.navigate().refresh();
        Thread.sleep(1000);
        // 浏览器前进
        driver.navigate().forward();
        Thread.sleep(1000);
        // 滚动当前页面的滚动条, 向下滚动 10000xp
        ((JavascriptExecutor) driver).executeScript("document.documentElement.scrollTop = 10000");
        // 调整为最大大小
        driver.manage().window().maximize();
        Thread.sleep(1000);
        // 全屏
        driver.manage().window().fullscreen();
        Thread.sleep(1000);
        // 设置大小(宽 600xp, 高 1000xp)
        driver.manage().window().setSize(new Dimension(600, 1000));
    }

    private static void test06() {
        ChromeOptions options = new ChromeOptions();
        // 允许所有的请求
        options.addArguments("--remote-allow-origins=*");
        ChromeDriver driver = new ChromeDriver(options);
        // 打开百度首页
        driver.get("https://www.baidu.com");

        // 判断元素是否可以被点击
        WebDriverWait wait = new WebDriverWait(driver, 1);
        // wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#bottom_layer > div111")));
        wait.until(ExpectedConditions.titleIs("百度一下，你就知道"));
    }

    private static void test05() {
        ChromeOptions options = new ChromeOptions();
        // 允许所有的请求
        options.addArguments("--remote-allow-origins=*");
        ChromeDriver driver = new ChromeDriver(options);
        // 打开百度首页
        driver.get("https://www.baidu.com");

        String url = driver.getCurrentUrl();
        String title = driver.getTitle();
        if (url.equals("https://www.baidu.com/") && title.equals("百度一下，你就知道")) {
            System.out.println("当前页面 url:" + url + ", 当前页面 title:" + title);
            System.out.println("测试通过");
        } else {
            System.out.println("测试不通过");
        }
    }

    private static void test04() {
        ChromeOptions options = new ChromeOptions();
        // 允许所有的请求
        options.addArguments("--remote-allow-origins=*");
        ChromeDriver driver = new ChromeDriver(options);
        // 打开百度首页
        driver.get("https://www.baidu.com");

        String buttonVal = driver.findElement(By.cssSelector("#su")).getAttribute("value");
        // System.out.println(buttonVal);
        if (buttonVal.equals("百度一下")) {
            System.out.println("测试通过");
        } else {
            System.out.println(buttonVal);
            System.out.println("测试不通过");
        }
    }

    private static void test03() {
        ChromeOptions options = new ChromeOptions();
        // 允许所有的请求
        options.addArguments("--remote-allow-origins=*");
        ChromeDriver driver = new ChromeDriver(options);
        // 打开百度首页
        driver.get("https://www.baidu.com");

        driver.findElement(By.xpath("//*[@id=\"s-top-left\"]/a[1]")).submit();
    }

    private static void test02() throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        // 允许所有的请求
        options.addArguments("--remote-allow-origins=*");
        ChromeDriver driver = new ChromeDriver(options);
        // 打开百度首页
        driver.get("https://www.baidu.com");
        // 找到百度搜索输入框, 输入 aaa
        driver.findElement(By.cssSelector("#kw")).sendKeys("aaa");
        // 点击百度一下按钮
        // submit 弊端:
        //      如果点击的元素放在 form 标签中, 此时使用的效果跟 click 是一样的
        //      放在非 form 标签中, 就会报错
        driver.findElement(By.cssSelector("#su")).submit();
        // 清空百度搜索的输入框中的数据
        // driver.manage().timeouts().implicitlyWait(3, TimeUnit.DAYS);
        driver.findElement(By.cssSelector("#kw")).clear();
    }

    private static void test01() throws InterruptedException {
        boolean flag = false;
        ChromeOptions options = new ChromeOptions();
        // 允许所有的请求
        options.addArguments("--remote-allow-origins=*");
        ChromeDriver driver = new ChromeDriver(options);
        // 打开百度首页
        driver.get("https://www.baidu.com");

        // 找到百度搜索输入框 (通过 css 选择器)
        // WebElement element = driver.findElement(By.cssSelector(".s_ipt"));
        // 通过 XPath
        WebElement element = driver.findElement(By.xpath("//*[@id=\"kw\"]"));

        // 输入软件测试
        element.sendKeys("软件测试");

        // 找到百度一下按钮
        // 点击
        driver.findElement(By.cssSelector("#su")).click();
        Thread.sleep(3000);
        // 校验
        // 找到搜索结果
        List<WebElement> aEm = driver.findElements(By.cssSelector("a em"));
        for (WebElement webElement : aEm) {
            // System.out.println(webElement.getText());
            // 如果返回的结果包含软件测试, 证明测试通过, 否则测试不通过
            if (webElement.getText().equals("软件测试")) {
                flag = true;
                System.out.println("测试通过");
                break;
            }
        }
        if (!flag) {
            System.out.println("测试不通过");
        }
    }
}